/**
 * @fileoverview Utility Bar module view. (Bottom fixed blue bar)
 * @author Chris Manning
 */
define([
    'jquery',
    'underscore',
    'backbone',
    'pubsub',
    'app',
    'state',
    'views/modules/share-tools',
    'views/modules/comment',
    'fwinfo',
    'facebook',
    'libs/onlineopinion/oo_engine'
],
function (
    $,
    _,
    Backbone,
    PubSub,
    App,
    StateManager,
    ShareToolsView,
    CommentView,
    FwInfo,
    Facebook,
    OOo
)
    {

        /**
         * View class.
         */
            var UtilityBarView = Backbone.View.extend({

            // View element.
            el: '.utility-wrap',

            // Events.
            events: {
                'click .util-bar-print-btn': 'printWindow',
                'click .util-bar-comment-btn': 'onClickCommentBtn',
                'click .util-bar-share-btn': 'onClickShareBtn',
                'click .util-bar-feedback-btn': 'onFeedbackBtn'
            },

            /**
             * Initialize view.
             * @param {Object} options View options passed during init.
             */
            initialize: function(options) {
                _.bindAll(this, 'parseCommentCount');
                this.options = $.extend({
                }, options);
                this.subviews = {};
                this.pubSub = {
                    'comment:created': this.parseCommentCount,
                    'comment:removed': this.parseCommentCount
                };
                PubSub.attach(this.pubSub, this);
                this.show();
                this.url = App.getPageUrl();
                this.parseCommentCount();
            },

            /**
             * Initalize comments module.
             */
            onClickCommentBtn: function(e) {
                if (!$(e.currentTarget).hasClass('open')) this.subviews.comments = new CommentView(
                    {
                        el: this.$('.util-bar-comment-module'),
                        event: e
                    });
            },

            /**
             * Initalize share module.
             */
            onClickShareBtn: function(e) {
                if (!$(e.currentTarget).hasClass('open')) this.subviews.share = new ShareToolsView(
                    {
                        el: this.$('.util-bar-share-module'),
                        event: e
                    });
            },

            onFeedbackBtn: function(e){
                (new OOo.Ocode({})).show();
            },

            /**
             * Open print dialog.
             */
            printWindow: function(e) {
                PubSub.trigger('uotrack', 'UtilityBarPrint');
                window.print();                
                return false;
            },

            /**
             * Fetch comment count and update on page.
             */
            parseCommentCount: function() {
                var $count = this.$('.util-bar-comment-count');
                Facebook.getFB(function(FB) {
                    FB.XFBML.parse($count[0]);
                });
            },

            /**
             * Adds show class to el.
             */
            show: function() {
                this.$el.addClass('show');
            },

            /**
             * Removes show class from el.
             */
            hide: function() {
                this.$el.removeClass('show');
            },

            /*
             * Destroy view.
             */
            destroy: function(removeEl){
                // Avoid jumpiness in Chrome due to fixed position bug.
                this.$el.css({
                    height : App.get('win').height() - StateManager.getBreakingNewsOffset(),
                    top : App.getScrollPosition()
                });
                this.$('.utility-bar').css({
                    position: 'absolute'
                });
                this.$('.util-bar-print-feedback').css({
                    position: 'absolute'
                });
                this.undelegateEvents();
                PubSub.unattach(this.pubSub, this);
                _.each(this.subviews, function(view){
                    if (typeof view.toggle !== 'undefined') view.toggle(undefined, true);
                    if (typeof view.destroy !== 'undefined') view.destroy(removeEl);
                });
                if (removeEl) this.remove();
            }
        });

        /**
         * Return view class.
         */
        return UtilityBarView;
    }
);
